import { S_CELL_GS_WIDTH, S_CELL_FALL_SPEED } from "./GameGSDefine";
cc.Class({
    extends: cc.Component,

    properties: {
        sprIcon: cc.Sprite,
    },

    init(cellModel) {
        this.setNormal();
        this.node.stopAllActions();
        this.node.scale = 1;

        if (!cellModel) {
            return;
        }
        this.setColor(cellModel.color);
        this.updatePosition(cellModel.x, cellModel.y);
    },

    // 更新cell的颜色
    setColor(color) {
        // 加载图片
        let url = 'textures_for_guosheng/star/block' + color;
        cc.loader.loadRes(url, cc.SpriteFrame, (err, res) => {
            if (err) {
                return;
            }
            this.sprIcon.spriteFrame = res;
        });
    },

    // 更新位置
    updatePosition(x, y) {
        let cellX = x * S_CELL_GS_WIDTH - 4.5 * S_CELL_GS_WIDTH;
        let cellY = y * S_CELL_GS_WIDTH - 4.5 * S_CELL_GS_WIDTH;
        this.node.x = cellX;
        this.node.y = cellY;
    },

    // 下落动画
    playFallEffect(y) {
        let cellY = y * S_CELL_GS_WIDTH - 4.5 * S_CELL_GS_WIDTH;
        cc.tween(this.node)
            .to(S_CELL_FALL_SPEED, { y: cellY }, { easing: 'backOut' })
            .start()
    },

    // 靠拢动画
    playGatherEffect(x) {
        let cellX = x * S_CELL_GS_WIDTH - 4.5 * S_CELL_GS_WIDTH;
        cc.tween(this.node)
            .to(S_CELL_FALL_SPEED, { x: cellX }, { easing: 'quadIn' })
            .start()
    },

    moveTo(x, y) {
        cc.tween(this.node)
            .to(S_CELL_FALL_SPEED, { x: x, y: y }, { easing: 'backOut' })
            .start()
    },

    recoverCellNode() {
        cc.tween(this.node)
            .to(0.2, { scale: 0 })
            .call(() => {
                F.Utils.putPoolFuckNode('nodePoolCell', this.node);
            })
            .start()
    },

    setGray() {
        F.Common.setGray(this.sprIcon.node);
    },
    setNormal() {
        F.Common.setNormal(this.sprIcon.node);
    },

});
